import { _decorator, Component, Node, Sprite, Vec3 } from 'cc';
import { GAME_SPRITEFRAME, GlobalData } from '../../../Data/GlobalData';
import { Tools } from '../../../Tool/Tools';
import { DragonBody } from '../DragonLoad/DragonBody';
const { ccclass, property } = _decorator;

@ccclass('SingleButtle')
/**
 * 子弹
 */
export class SingleButtle extends Component {

    //子弹样式
    @property(Sprite)
    private ButtleStyle: Sprite = null;

    //跟随角色
    public FllowNode: Node = null;

    /**
     * 更新子弹
     */
    public UpdateButtle(_buttleindex: number, _fllownode: Node) {
        this.FllowNode = _fllownode;
        this.ButtleStyle.spriteFrame = app.resManager.SpriteFrames[GAME_SPRITEFRAME.Buttle + _buttleindex];
    }

    /**
     * 更新位置
     */
    public UpdatePosition() {
        this.node.angle = Tools.ReturnAngle(this.node.worldPosition, this.FllowNode.worldPosition) - 90;

        this.node.setWorldPosition(
            this.node.getWorldPosition().add(
                new Vec3(
                    Math.cos((this.node.angle + 90) * Math.PI / 180) * GlobalData.ButtleSpeed,
                    Math.sin((this.node.angle + 90) * Math.PI / 180) * GlobalData.ButtleSpeed
                )
            )
        );
    }

    /**
     * 返回是否碰撞了
     */
    public ReturnIsHit(): boolean {
        if (Tools.isCircleIntersect(this.node.getWorldPosition(), 0, this.FllowNode.getWorldPosition(), 20)) {
            this.FllowNode.getComponent(DragonBody).IamDisappear();
            return true;
        }
        return false;
    }

}


